package com.maple.common.utils.agreements;
import com.maple.common.utils.Tools;
import android.util.Log;

import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class B11AgreementUtil {
    /**
     * 截取字符串
     * */
    public static void testRegexp(String s,CallBackWeight callBackWeight) {
        byte[]data1=Tools.parseHexStr2Byte(s);
        String s1=dataTransition(data1);
        Log.i("pqc", "解析后数据："+s1);
        if (s != null && !"".equals(s1)) {
//            String regex = "53D4AC4753AC[0-9a-fA-F]{16}EBE78D0A";
            String regex = "[0-9a-fA-F]{28}6B670D0A";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(s1);
            while (matcher.find()) {
                String matcherString = matcher.group();
                System.out.println(matcherString);
                byte[]data=Tools.parseHexStr2Byte(matcherString);
                StringBuilder builder=new StringBuilder();
                for (int i = 8; i < data.length-4; i++) {
                    builder.append((char) data[i]);
                }
                String res=builder.toString();
                System.out.println(res);
                try {
//                    String w=res.substring(0,res.length()-2);
                    BigDecimal decimal=new BigDecimal(res.replaceAll(" ",""));
                    System.out.println(decimal.toString());
                    callBackWeight.succeed(decimal.toString());
                }catch (Exception e){
                    System.out.println("解析错误");
                }

            }
        }
    }

    /**
     * 数据转换
     * */
    public static String dataTransition(byte[]datas){
        StringBuilder data=new StringBuilder();
        for(int i=0;i<datas.length;i++){
            int num=0;
            int DataTemp1=datas[i];
            int DataTemp2=DataTemp1;
            for(int j =0;j<7;j++){
                if((DataTemp2&1)==1){
                    num++;
                }
                DataTemp2=(DataTemp2>>1);
            }
            if(num%2!=0){
                DataTemp1=DataTemp1&0x7F;
            }
            System.out.println("int:"+DataTemp1);
            System.out.println(Integer.toHexString(DataTemp1).toUpperCase());
            if (DataTemp1<16){
                data.append("0"+Integer.toHexString(DataTemp1).toUpperCase());
            }else {
                data.append(Integer.toHexString(DataTemp1).toUpperCase());
            }
        }
        return data.toString();
    }


}
